<?php

//print_r($_POST);
 
// username and password sent from form 

		$result=array();


			runtimelog(print_r($_POST,TRUE));
if(isset($_POST['orgid'])&&isset($_POST['option'])&&$_POST['option']=="get_count"){
	$orgid=$_POST['orgid'];
	$sql="SELECT
		inv.number as 'num1',
		0 as 'num2',
		inv.scan_code as 'scan_code',
		i.product_code as 'product_code',
		i.designer_code as 'design_code',
		(SELECT  `value` 
		FROM  `_c_item` 
		WHERE  `name` =  'itemType'
		AND  `para` =  i.type) as 'type', 
		(SELECT  `value` 
		FROM  `_c_item` 
		WHERE  `name` =  'itemYear'
		AND  `para` =  i.year) as 'year',
		(SELECT  `value` 
		FROM  `_c_item` 
		WHERE  ((`name` =  'itemSeason' AND CHAR_LENGTH(inv.scan_code)!=12)
		OR (`name` =  'itemSeason4' AND CHAR_LENGTH(inv.scan_code)=12))
		AND  `para` =  i.season)as 'season',
		i.flow as 'flow',
		(SELECT  `value` 
		FROM  `_c_item` 
		WHERE  `name` =  'itemMaterial'
		AND  `para` =  i.material) as 'material',
		(SELECT  `value` 
		FROM  `_c_item` 
		WHERE  `name` =  'itemColor'
		AND  `para` =  i.color) as 'color',
		(SELECT  `value` 
		FROM  `_c_item` 
		WHERE  `name` =  'itemSize'
		AND  `para` =  i.size) as 'size',
		inv.id as 'touserid',
		inv.`time_stamp` as 'hide'
		FROM inventory inv 
		inner join item i on i.`scan_code` =inv.`scan_code` 
		WHERE  inv.`orid` =$orgid
		AND  inv.`number` >0";
		runtimelog("option is get_stack".$sql);
		$sqlResult=mysql_query($sql);
		$result['inventorys'] = array ();
	        while($row = mysql_fetch_array($sqlResult))
	        {
	         array_push($result['inventorys'],$row );
	        }
			echo json_encode($result);
	 		 return;
		}
		if(!isset($_POST['userid'])||!isset($_POST['orgid'])||!isset($_POST['inventorys'])){
	
			runtimelog("uncomplete");	
			$result['stat']=0;
			echo json_encode($result);
	 		 return;
		}
		
		$inventorys=$_POST['inventorys'];
		$userid=$_POST['userid'];
		$orgid=$_POST['orgid'];
		$error=array();
		foreach($inventorys as $inv){
			
		
			$scan_code=$inv['scan_code'];
			if(!isset($inv['touserid'])){
				array_push($error,$inv['touserid']);
				continue;
			}
			$inv_id=$inv['touserid'];
			$num=$inv['num2'];
			if($inv['num2']!=$inv['num1']){
				array_push($error,$inv['touserid']);
				continue;
			}
			$sql="UPDATE inventory SET `time_stamp`=now() WHERE id= $inv_id";
			$sqlresult=mysql_query($sql);
			if($sqlresult){
				
			}else{
				array_push($error,$inv_id);
			}
			
		}
		
		$result['stackedNum']=count($inventorys);
		$result['failededNum']=count($error);
		$result['error']=$error;
		$result['stat']=1;
		echo json_encode($result);
	 	return;

?>